﻿using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.Depo._7C
{
    public partial class xrCikilanEnler : DevExpress.XtraReports.UI.XtraReport
    {
        public xrCikilanEnler()
        {
            InitializeComponent();
        }

        DataSet ds;
        public xrCikilanEnler(int CikisPartisiId)
            : this()
        {
            lblHeader.Text = CikisPartisiId.ToString() + " nolu Çıkış Partisindeki Kumaş Enleri";

            ds = Session.DataManager.ExecuteDataSet("GetKesimEnRapor", CikisPartisiId);

            xrSubModel srModel = (xrSubModel)SubModel.ReportSource;
            xrSubEn srEn = (xrSubEn)SubEn.ReportSource;

            srModel.fillTable(ds.Tables["Table1"]);
            srEn.fillTable(ds.Tables["Table2"]);

            tcAtelye.Text = ds.Tables["Table3"].Rows[0]["Atelye"].ToString();

        }

        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            this.DataMember = "Table";
            this.DataSource = ds;

            tcArticle.DataBindings.Add("Text", null, "Table.Product");
            pbScan.DataBindings.Add("Image", null, "Table.Photo3");

            Detail.HeightF = 180F;
            xrTable2.HeightF = 180F;
        }

        private void xrTable2_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            xrTable2.HeightF = 180F;
            Detail.HeightF = 180F;
            string pid = GetCurrentColumnValue("ProductId").ToString();
            int a = ds.Tables["Table1"].Select("ProductId=" + pid).Length;
            int b = ds.Tables["Table2"].Select("ProductId=" + pid).Length;
            if (a > 3 || b > 3)
            {
                xrTable2.HeightF = a > b ? a * 60 : b * 60;
            }
        }

        private void SubEn_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ((xrSubEn)((XRSubreport)sender).ReportSource).PrdId.Value =
                    GetCurrentColumnValue("ProductId").ToInt();
        }

        private void SubModel_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ((xrSubModel)((XRSubreport)sender).ReportSource).PrdId.Value =
                    GetCurrentColumnValue("ProductId").ToInt();
        }

        private void pbScan_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            //pbScan.WidthF = xrTableCell2.WidthF;
        }

        private void ReSize(object sender)
        {
            //Resize ı düzgün yapan tek fonksiyon !!!
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            int factor = 10;

            XRLabel label = (XRLabel)sender;

            float newSize = 15f;
            //Font defaultFont = new System.Drawing.Font("Trebuchet MS", newSize);
            Font defaultFont = new System.Drawing.Font("Calibri", newSize);

            float stringWidth = gr.MeasureString(label.Text, defaultFont).Width;
            float labelWidth = label.WidthF;


            if (labelWidth < stringWidth * factor)
                newSize = defaultFont.Size * label.WidthF / stringWidth / factor;


            label.Font = new Font(defaultFont.FontFamily, newSize);
            gr.Dispose();
        }

        private void tcArticle_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }
    }
}
